package com.oxygen.pt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.oxygen.pt.mapper.ChamberInspectionMonthlyMapper;
import com.oxygen.pt.pojo.dto.ChamberInspectionMonthlySaveDTO;
import com.oxygen.pt.pojo.entity.ChamberInspectionMonthly;
import com.oxygen.pt.pojo.vo.ChamberInspectionMonthlyVO;
import com.oxygen.pt.service.ChamberInspectionMonthlyService;
import com.oxygen.pt.utils.InspectionRemarkConverter;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ChamberInspectionMonthlyServiceImpl implements ChamberInspectionMonthlyService {

    @Resource
    private ChamberInspectionMonthlyMapper mapper;

    /* ---------- 基础 CRUD ---------- */
    @Override
    public Long save(ChamberInspectionMonthlySaveDTO dto) {
        // 1. 快速拷贝同名同类型字段
        ChamberInspectionMonthly entity = new ChamberInspectionMonthly();
        BeanUtil.copyProperties(dto, entity);

        // 2. 把零散备注转 JSON
        entity.setRemarksJson(InspectionRemarkConverter.buildRemarksJson(dto));

        // 3. 落库
        mapper.insert(entity);

        // 4. 返回主键
        return entity.getId();
    }

    @Override
    public int remove(Long id) {
        return mapper.deleteById(id);
    }

    @Override
    public int modify(ChamberInspectionMonthly record) {
        return mapper.updateById(record);
    }

    @Override
    public ChamberInspectionMonthly findById(Long id) {
        return mapper.selectById(id);
    }

    /* ---------- 分页 ---------- */
    @Override
    public PageInfo<ChamberInspectionMonthlyVO> page(int pageNum,
                                                     int pageSize,
                                                     Long chamberId,
                                                     LocalDateTime startTime,
                                                     LocalDateTime endTime) {
        // PageHelper 核心：线程本地变量设置分页参数
        PageHelper.startPage(pageNum, pageSize);
        // 查询
        List<ChamberInspectionMonthlyVO> list = mapper.pageQuery(chamberId, startTime, endTime);
        // 包装为 PageInfo（包含 total、pages、list 等）
        return new PageInfo<>(list);
    }

    @Override
    public Map<String, Object> buildWordData(Long chamberId, Integer year){
        List<ChamberInspectionMonthly> list = mapper.selectByYear(chamberId, year);

        // 12 个月占位容器
        Map<String,Object> data = new HashMap<>();

        // 先把 12 个月置空
        for(int m=1; m<=12; m++){
            String mm = String.format("%02d", m);
            data.put("ups_"        + mm, "");
            data.put("window_"     + mm, "");
            data.put("elec_"       + mm, "");
            data.put("pipe_"       + mm, "");
            data.put("decom_"      + mm, "");
            data.put("sprinkler_"  + mm, "");
            data.put("tank_"       + mm, "");
            data.put("compressor_" + mm, "");
            data.put("vacuum_"     + mm, "");
            data.put("vessel_"     + mm, "");
            data.put("inspector_"  + mm, "");
            data.put("remark_"     + mm, "");
        }

        // 填充真实值
        for(ChamberInspectionMonthly m : list){
            String mm = String.format("%02d", m.getInspectMonth());

            // 状态：0=正常，1=异常 → 空/★
            data.put("ups_"        + mm, m.getUpsStatus() == 1 ? "异常" : "正常");
            data.put("window_"     + mm, m.getObservationWindowStatus() == 1 ? "异常" : "正常");
            data.put("elec_"       + mm, m.getElectricalSystemStatus() == 1 ? "异常" : "正常");
            data.put("pipe_"       + mm, m.getPressurePipeStatus() == 1 ? "异常" : "正常");
            data.put("decom_"      + mm, m.getEmergencyDecompressStatus() == 1 ? "异常" : "正常");
            data.put("sprinkler_"  + mm, m.getFireSprinklerStatus() == 1 ? "异常" : "正常");
            data.put("tank_"       + mm, m.getWaterTankStatus() == 1 ? "异常" : "正常");
            data.put("compressor_" + mm, m.getAirCompressorStatus() == 1 ? "异常" : "正常");
            data.put("vacuum_"     + mm, m.getNegativePumpStatus() == 1 ? "异常" : "正常");
            data.put("vessel_"     + mm, m.getContainerWorkStatus() == 1 ? "异常" : "正常");

            // 巡检人
            data.put("inspector_" + mm,
                    m.getInspector() == null ? "" : m.getInspector());

            // 当月所有备注（JSON -> 字符串）
            data.put("remark_" + mm,
                    InspectionRemarkConverter.extractMonthRemark(m.getRemarksJson()));
        }
        return data;
    }

    @Override
    public ChamberInspectionMonthlySaveDTO findDtoById(Long id) {
        ChamberInspectionMonthly entity = mapper.selectById(id);
        if (entity == null) {
            return null;
        }
        // 实体 → DTO
        ChamberInspectionMonthlySaveDTO dto = new ChamberInspectionMonthlySaveDTO();
        BeanUtil.copyProperties(entity, dto);
        // 把 JSON 备注拆回零散字段
        InspectionRemarkConverter.fillRemarkFields(entity.getRemarksJson(), dto);
        return dto;
    }
}